package com.qfmy.mystation.api;


import android.content.Context;
import android.content.SharedPreferences;

import androidx.preference.PreferenceManager;

import com.qfmy.mystation.MyStationInit;
import com.qfmy.mystation.bean.LiveWallpaperBean;
import com.qfmy.mystation.result.CategoryResult;
import com.qfmy.mystation.result.WallpaperResult;

import io.reactivex.Flowable;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Query;

/**
 * 描述：API 请求接口
 */

public class APIService {

    private static String BASE_URL = "http://47.98.172.89:8080";   //未启用

    public static void setHost(Context context) {
        final String DEFAULT_IP = "47.98.172.89";
        final String DEFAULT_PORT = "8080";
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        String ip = preferences.getString("ip", DEFAULT_IP);
        String port = preferences.getString("port", DEFAULT_PORT);
        BASE_URL = String.format("http://%s:%s", ip, port);
    }

    public APIs apis;

    private static APIService instance;

    public static APIService getInstance() {
        if (instance == null) {
            instance = new APIService();
        }
        return instance;
    }

    private APIService() {
        Retrofit storeRestAPI = new Retrofit.Builder().baseUrl(BASE_URL)
                .client(MyStationInit.mOkHttpClient)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        apis = storeRestAPI.create(APIs.class);
    }

    public interface APIs {
        @GET("/wallpaper/getWallpaperByCategory")
        Flowable<WallpaperResult> getWallpaperByCategory(
                @Query("page") int page,
                @Query("category") String category,
                @Query("count") int count
        );

        @GET("/wallpaper/getLiveWallpaperByCategory")
        Flowable<LiveWallpaperBean> getLiveWallpaperByCategory(
                @Query("page") int page,
                @Query("category") String category,
                @Query("count") int count
        );

        @GET("/wallpaper/search")
        Flowable<WallpaperResult> getData(
                @Query("keyword") String keyword
        );

        @GET("/wallpaper/categories")
        Flowable<CategoryResult> getCategories();

        @GET("/wallpaper/liveCategories")
        Flowable<CategoryResult> getLiveCategories();
    }
}
